package io.web.bi;

import com.qcloud.cos.internal.RequestClientOptions;
import org.apache.http.HttpHost;
import org.aspectj.lang.annotation.Before;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * RestHighLevelClientDemo.
 *
 * @date 2023-07-18
 */
public class RestHighLevelClientDemo {

    RestHighLevelClient client;

    @BeforeEach
    public void beforeEach() throws IOException {
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
    }

    @AfterEach
    public void afterEach() throws IOException {
        client.close();
    }

    @Test
    public void index() throws Exception {
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("user", "kimchy");
        jsonMap.put("postDate", new Date());
        jsonMap.put("message", "trying out Elasticsearch");
        IndexRequest indexRequest = new IndexRequest("posts")
                .id("1").source(jsonMap);
        IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println();
    }
}
